﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by #COMPANY# 
 *All rights reserved. 
 *FileName:    
 *Author:       qsgy
 *Version:     
 *UnityVersion：#UNITYVERSION# 
 *Date:         #DATE# 
 *Description:    玩家控制器。暂时用pc端的A键控制移动
 * 必须刚体。玩家一直加速，按键改变速度方向
 * 尾部火焰利通粒子效果。方向用其父物体旋转达到效果。
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 控制玩家移动
/// 参考汽车启动加速模式，按健touch右半边屏幕触发转向
/// </summary>
public class MoveControl : NextRunner {
    public Animator animator;
    public Rigidbody rig;
    [Header("Property")]
    [Tooltip("模拟汽车启动的功率")]
    public float power = 150f;
    [Tooltip("运动阻力。阻力越大，启动速度越小，最大速度越小。相当于摩擦阻力，其值影响最终的速度")]
    public float resistance = 1.5f;
    [Tooltip("跳跃初速度"),SerializeField,Range(0,100f)]
    private float jumpV = 5f;
    [Space(10)]
    [Header("state")]
    [Tooltip("是否顺时针旋转")]
    public bool isRockWise = true;
    [Tooltip("是否改变方向")]
    private bool isChange;
    [Tooltip("是否停止")]
    private bool isStop;
    [Tooltip("是否加速状态")]
    public bool isAccelerate = false;
    [Tooltip("是否处于反弹")]
    private bool isRebound=false;
    [Tooltip("实时速度")]
    public float velocity=0;
    [Tooltip("面朝的方向")]
    public Direction dir = Direction.Ahead;
    [SerializeField]
    private float force = 0;//加速牵引力

    
    
    public static Vector3[] eEect=new Vector3[4] {new Vector3(0,-1,0),new Vector3(-1,0,0),
                    new Vector3(0,1,0),new Vector3(1,0,0)};//分别对应四个方向的向量

    
    public Transform fire;
    public Direction Dir
    {
        get
        {
            return dir;
        }

        set
        {
            switch ((int)value-(int)dir)
            {
                case 1:
                fire.Rotate(new Vector3(0, 0, -90));
                    break;
                case 2:
                fire.Rotate(new Vector3(0, 0, 180));
                    break;
                case 3:
                    fire.Rotate(new Vector3(0, 0, 90));
                    break;
                case -1:
                    fire.Rotate(new Vector3(0, 0, 90));
                    break;
                case -2:
                    fire.Rotate(new Vector3(0, 0, 180));
                    break;
                case -3:
                    fire.Rotate(new Vector3(0, 0, -90));
                    break;
            }
            dir = value;
            animator.SetInteger("dir", (int)dir);//更改动画机的参数
        }
    }//方向使用属性
    public bool IsChange
    {
        get { return isChange; }
        set { isChange = value; }
    }//是否旋转的属性
    public bool IsStop
    {
        get { return isStop; }
        set { isStop = value; }
    }//是否停下来的属性
    public bool IsReBound
    {
        get { return isRebound; }
        set { isRebound = value; }
    }

     void Awake()
     {
        //初始化组件
        animator = GetComponentInChildren<Animator>();//从子物体获取动画组件
        rig = GetComponent<Rigidbody>();
#if DEBUG
        this.ProbeComponent(ref rig, "player not find rigdbody");

#endif
        fire = transform.Find("Animation").Find("Fire");
        this.ProbeIsNull(fire, true, "Player fire don't find");//火焰判空

        isStop = false;
        isChange = true;




        
    }


    //安卓输入和键盘输入
    void Update () {
        
        if (Time.deltaTime != 0)
        {

            if (Input.GetMouseButtonDown(0))//如果有触屏
            {

                if (!IsPointerOverUIObject(0) && IsChange)
                {
                    ChangeDir();
                }
                /*else
                {
                    print("mouse pos :" + Input.mousePosition);
                   

                    //实例化点击事件  
                    PointerEventData eventDataCurrentPosition = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
                    //将点击位置的屏幕坐标赋值给点击事件  
                    eventDataCurrentPosition.position = Input.GetTouch(0).position;

                    List<RaycastResult> results = new List<RaycastResult>();
                    //向点击处发射射线  
                    EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
                    // Debug.Log(results[0].gameObject.tag);
                    if (results[0].gameObject.tag=="GameController")
                    {
                        ChangeDir();

                    }
                    print(results[0].gameObject.name);
                }*/

            }

#if         DEBUG

            if (Input.GetKeyDown(KeyCode.A))//电脑空格模拟手机按键
            {
                
                    // print("backspace key down");
                    ChangeDir();
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Jump();
            }
#endif
            //加速
            velocity = rig.velocity.magnitude;
            force = power / (velocity + resistance);
            if ((int)dir < 4)
                rig.AddForce(force * eEect[(int)dir]);
        }
    }

    


    public bool IsPointerOverUIObject(int fingerID)
    {
        return EventSystem.current.IsPointerOverGameObject(fingerID);
    }
    /// <summary>
    /// 跳跃
    /// </summary>
    public void Jump()
    {
        
        
        rig.velocity = rig.velocity + new Vector3(0, 0, -jumpV);

        
    }

    #region Direction
    /// <summary>
    /// 改变方向
    /// </summary>
    public void ChangeDir()
    {
        if (isChange)
        {
            if (isRockWise)
            {
                WiseDir();
            }
            else
            {
                Anti_WiseDir();
            }
        }
    }

    /// <summary>
    /// 顺时针旋转dir
    /// </summary>
    void WiseDir()
    {
        if ((int)dir>2)
        {
            Dir = Direction.Ahead;
        }
        else
        {
            Dir = (Direction)((int)Dir + 1);
        }
    }

    /// <summary>
    /// 逆时针旋转dir
    /// </summary>
    void Anti_WiseDir()
    {
        if ((int)dir < 1)
        {
            Dir = Direction.Right;
        }
        else
        {
            Dir = (Direction)((int)Dir - 1);
        }
    }
    #endregion
    /// <summary>
    /// 修改power
    /// </summary>
    public void ModifyPower()
    {
        power = 150f;
    }
}

